﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers.LoaiDoiTuongTroCapService;
using eTMS.Controllers;
using eTMS.Commons;
using eTMS.Controllers.KhoanTienService;
namespace eTMS.Forms.ManageList
{
    public partial class FrmLoaiDoiTuongTroCap : eTMS.Controls.BaseForm
    {
        public FrmLoaiDoiTuongTroCap()
        {
            InitializeComponent();
        }
        #region Variables
        private LoaiDoiTuongTroCapController controller = new LoaiDoiTuongTroCapController();
        #endregion
        #region Screen Functions
        //Load All lable text
        private void LoadLanguage()
        {
            this.Text = LoaiDoiTuongTroCapStringMapping.LoaiDoiTuongTroCap;
            this.labelFormTitle.Text = Constants.APP_ADMINISTRATION + " " + LoaiDoiTuongTroCapStringMapping.LoaiDoiTuongTroCap;
            this.groupBoxDetail.Text = Constants.APP_DETAIL;
            this.groupBoxList.Text = Constants.APP_LIST;
            this.ColumnSTT.HeaderText = Constants.APP_INDEX;
            labelTenLoaiDoiTuong.Text = LoaiDoiTuongTroCapStringMapping.TenLoaiDoiTuong;
            ColumnTenLoaiDoiTuong.HeaderText = LoaiDoiTuongTroCapStringMapping.TenLoaiDoiTuong;
            labelGhiChu.Text = LoaiDoiTuongTroCapStringMapping.GhiChu;
            ColumnGhiChu.HeaderText = LoaiDoiTuongTroCapStringMapping.GhiChu;
          
        }
        //Get entity selected from datagrid
        private LoaiDoiTuongTroCapEntity GetObject()
        {
            LoaiDoiTuongTroCapEntity o = controller.MapDataRowToObject(((DataRowView)gridData.SelectedRows[0].DataBoundItem).Row);
            return o;
        }
        //Get entites selected from datagrid
        private List<DataRow> GetSelectedDataRows()
        {
            List<DataRow> ls = new List<DataRow>();
            for (int i = 0; i < gridData.SelectedRows.Count; i++)
            {
                ls.Add(GetDataRow(gridData.SelectedRows[i].Index));
            }
            return ls;
        }
        //Get the selected data row in datagrid
        private DataRow GetSelectedDataRow()
        {
            DataRow row = GetDataRow(gridData.SelectedRows[0].Index);
            return row;
        }
        //Get the selected data row in datagrid
        private DataRow GetDataRow(int index)
        {
            DataRow row = ((DataRowView)gridData.Rows[index].DataBoundItem).Row;
            return row;
        }
        //Reset all textbox
        private void ClearScreen()
        {
            txtTenLoaiDoiTuong.Text = "";
            txtGhiChu.Text = "";
            errorProviderData.Clear();
        }
        //Lock-Unlock control in screen
        private void LockScreen(bool IsLocked)
        {
            ucCommand.LockControl(IsLocked);
            txtTenLoaiDoiTuong.ReadOnly = IsLocked;
            txtGhiChu.ReadOnly = IsLocked;
            btnAddDetail.Enabled = !IsLocked;
            btnRemoveDetail.Enabled = !IsLocked;
            gridChiTiet.ReadOnly = IsLocked;
        }
        //Copy data from control in screen to object
        private void CopyFromSreen(LoaiDoiTuongTroCapEntity o)
        {
            o.TenLoaiDoiTuong = Convert.ToString(txtTenLoaiDoiTuong.Text);
            o.GhiChu = Convert.ToString(txtGhiChu.Text);
        }
        //Bind data from object to control in screen
        private void CopyToSreen(LoaiDoiTuongTroCapEntity o)
        {
            if (!o.IsNullTenLoaiDoiTuong)
            {
                txtTenLoaiDoiTuong.Text = Convert.ToString(o.TenLoaiDoiTuong);
            }
            if (!o.IsNullGhiChu)
            {
                txtGhiChu.Text = Convert.ToString(o.GhiChu);
            }
        }
        //Validate all data from control in screen before Save data
        protected bool ValidateScreen()
        {
            bool result = true;
            if (txtTenLoaiDoiTuong.Text.Trim() == "")
            {
                txtTenLoaiDoiTuong.Focus();
                errorProviderData.SetError(txtTenLoaiDoiTuong, Constants.MSS_ERROR_TEXT);
                result = false;
            }
            else
            {
                errorProviderData.SetError(txtTenLoaiDoiTuong, "");
            }
            if (!result)
            {
                ShowInfo(Constants.MSS_REQUIRE_FIELD);
            }
            return result;
        }
        //Copy data from selected object to control in screen
        private void BindInfo()
        {
            if (gridData.SelectedRows.Count == 0) return;
            if (gridData.SelectedRows[0].Index < gridData.Rows.Count)
            {
                LoaiDoiTuongTroCapEntity o = GetObject();
                CopyToSreen(o);
                controller.LoaiDoiTuongTroCapObject = o;
                controller.LoadChiTiet();
                gridChiTiet.AutoGenerateColumns = false;
                gridChiTiet.DataSource = controller.TableChiTiet;
            }
        }
        #endregion
        #region Business
        //AddNew data to database
        private bool AddNewData()
        {
            bool rval = false;
            controller.ResetObject();
            CopyFromSreen(controller.LoaiDoiTuongTroCapObject);
            rval = controller.AddNew();
            if (rval)
            {
                controller.SaveDetail();
                
                DataRow row = controller.Table.NewRow();
                row["STT"] = controller.Table.Rows.Count + 1;
                controller.MapObjectToDataRow(row);
                controller.Table.Rows.Add(row);
                gridData.DataSource = null;
                gridData.DataSource = controller.Table;
            }
            return rval;
        }
        //Update data to database
        private bool UpdateData()
        {
            DataRow row = GetSelectedDataRow();
            controller.LoadObject((Int32)row["LoaiDoiTuongTroCap_ID"]);
            CopyFromSreen(controller.LoaiDoiTuongTroCapObject);
            bool rval = controller.Update();
            if (rval)
            {
                controller.SaveDetail();
                
                controller.MapObjectToDataRow(row);
                gridData.InvalidateRow(gridData.SelectedRows[0].Index);
            }
            return rval;
        }
        //Delete data from database
        private bool DeleteData()
        {
            List<DataRow> ls = GetSelectedDataRows();
            if (ls.Count == 0) return false;
            bool rval = true;
            try
            {
                gridData.DataSource = null;
                foreach (DataRow row in ls)
                {
                    if (controller.DeleteByID((Int32)row["LoaiDoiTuongTroCap_ID"]))
                    {
                        this.controller.Table.Rows.Remove(row);
                    }
                    else
                    {
                        rval = false;
                    }
                }
                this.controller.ResetTableIndex();
                gridData.DataSource = this.controller.Table;
            }
            catch
            {
                return false;
            }
            return true;
        }
        #endregion
        #region DataBinding
        //Bind data to datagrid
        private void LoadData()
        {
            gridData.AutoGenerateColumns = false;
            gridData.DataSource = controller.Table;
            gridData.AutoResizeColumns();
        }
        #endregion
        #region Windows Control EventHandlers
        private void FrmLoaiDoiTuongTroCap_Load(object sender, EventArgs e)
        {
            KhoanTienController.LoadAllKhoanTien(false);
            List<KhoanTienEntity> ls = new List<KhoanTienEntity>(KhoanTienController.ListKhoanTienStatic.ToArray());
            KhoanTienEntity khoanTienEntity = new KhoanTienEntity();
            khoanTienEntity.KhoanTien_ID = 0;
            khoanTienEntity.TenKhoanTien = eTMS.Commons.Constants.APP_CHOOSE;
            khoanTienEntity.MaKhoanTien = eTMS.Commons.Constants.APP_CHOOSE;
            ls.Insert(0, khoanTienEntity);
            ColLoaiKhoanTien.DisplayMember = "TenKhoanTien";
            ColLoaiKhoanTien.ValueMember = "KhoanTien_ID";
            ColLoaiKhoanTien.DataSource = ls;

            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(Byte));
            table.Columns.Add("Name", typeof(string));
            DataRow r = table.NewRow();
            r["ID"] = 1;
            r["Name"] = "Phần trăm";
            table.Rows.Add(r);
            r = table.NewRow();
            r["ID"] = 2;
            r["Name"] = "Số tiền";
            r = table.NewRow();
            r["ID"] = 3;
            r["Name"] = "Trợ cấp";
            table.Rows.Add(r);
            ColCachTinh.DisplayMember = "Name";
            ColCachTinh.ValueMember = "ID";
            ColCachTinh.DataSource = table;


            LoadLanguage();
            LoadData();
            LockScreen(true);
        }
        private void gridData_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            BindInfo();
        }
        private void ucCommand_OnAddNew(object sender, EventArgs e)
        {
            ClearScreen();
            controller.ResetObject();
            controller.LoadChiTiet();
            gridChiTiet.AutoGenerateColumns = false;
            gridChiTiet.DataSource = controller.TableChiTiet;
            
            LockScreen(false);
            Flag = ACTIONENUM.AddNew;
            txtTenLoaiDoiTuong.Focus();
        }
        private void ucCommand_OnDelete(object sender, EventArgs e)
        {
            if (gridData.SelectedRows.Count == 0)
            {
                ShowInfo(Constants.MSS_CHOOSERECORD_REQUIRE);
                return;
            }
            if (!Confirm(Constants.MSS_CONFIRM_DELETE)) return;
            DeleteData();
            this.gridData.Focus();
        }
        private void ucCommand_OnEdit(object sender, EventArgs e)
        {
            if (gridData.SelectedRows.Count == 0)
            {
                ShowInfo(Constants.MSS_CHOOSERECORD_REQUIRE);
                return;
            }
            LockScreen(false);
            Flag = ACTIONENUM.Edit;
            txtTenLoaiDoiTuong.Focus();
        }
        private void ucCommand_OnSave(object sender, EventArgs e)
        {
            if (!ValidateScreen())
                return;
            if (Flag == ACTIONENUM.Edit)
            {
                if (UpdateData())
                {
                    ShowInfo(Constants.MSS_SAVE_SUCCESS);
                }
                else
                {
                    ShowWarning(Constants.MSS_SAVE_FAILE);
                }
                gridData.Focus();
            }
            else
            {
                if (AddNewData())
                {
                    ShowInfo(Constants.MSS_SAVE_SUCCESS);
                }
                else
                {
                    ShowWarning(Constants.MSS_SAVE_FAILE);
                }
            }
            LockScreen(true);
            Flag = ACTIONENUM.None;
        }
        private void ucCommand_OnSkip(object sender, EventArgs e)
        {
            LockScreen(true);
            if (Flag == ACTIONENUM.AddNew)
            {
                ClearScreen();
                ucCommand.SetFocusAddNew();
            }
            else if (Flag == ACTIONENUM.Edit)
            {
                BindInfo();
                this.gridData.Focus();
            }
            Flag = ACTIONENUM.None;
        }
        #endregion
        private void btnAddDetail_Click(object sender, EventArgs e)
        {
            controller.AddNewChiTiet();
        }

        private void btnRemoveDetail_Click(object sender, EventArgs e)
        {
            List<DataRow> ls = new List<DataRow>();
            foreach (DataGridViewRow row in gridChiTiet.SelectedRows)
            {
                ls.Add(((DataRowView)row.DataBoundItem).Row);
            }
            controller.RemoveChiTiet(ls);
        }

    }
}
